home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / parsingcommandline / example4.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  11KB  |  274 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Parsing Command Line        Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-06                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how you can create a RDArgs structure */
  21. /* yourself and prepare it before you parse the command line with  */
  22. /* the help of the ReadArgs() function. Since we can prepare the   */
  23. /* RDArgs structure we can include extra help (will be displayed   */
  24. /* if the user types "?" to display the command line template and  */
  25. /* then types "?" again.), decide if the user should be able to    */
  26. /* see the command line template, etc...                           */
  27.  
  28.  
  29.  
  30. /* Include the dos library definitions: */
  31. #include <dos/dos.h>
  32.  
  33. /* Include information about the argument parsing routine: */
  34. #include <dos/rdargs.h>
  35.  
  36. /* Now we include the necessary function prototype files:         */
  37. #include <clib/dos_protos.h>       /* General dos functions...    */
  38. #include <clib/exec_protos.h>      /* System functions...         */
  39. #include <stdio.h>                 /* Std functions [printf()...] */
  40. #include <stdlib.h>                /* Std functions [exit()...]   */
  41.  
  42.  
  43.  
  44. /* Here is our command line template. This program handles three      */
  45. /* types of command templates:                                        */
  46. /*                                                                    */
  47. /* 1. "SoundFile/A" The ReadArgs() expects one file name, else the    */
  48. /*                  function will fail. Since there is no "/M"        */
  49. /*                  option only one file name may be given.           */
  50. /*                                                                    */
  51. /* 2. V=Volume/K/N" The second type of argument is optional (no "/A"  */
  52. /*                  option. It must be a number ("/N" - Number option */
  53. /*                  is set) and preceded by the keyword "Volume" or   */
  54. /*                  "V" ("/K" - Keyword required). If a keyword is    */
  55. /*                  needed the user can either write the keyword a    */
  56. /*                  space and then the number, or the user may write  */
  57. /*                  the keyword an equal sign (=) and then the        */
  58. /*                  number. Please note that the "V=Volume" only      */
  59. /*                  means that the user can write "V" instead of the  */
  60. /*                  longer keyword "Volume", and this equal sign has  */
  61. /*                  nothing to do with the optional equal sign the    */
  62. /*                  user may write after the keyword and before the   */
  63. /*                  number. (No decimal numbers, e.g. "4.57", "1.2",  */
  64. /*                  are accepted.)                                    */
  65. /*                                                                    */
  66. /* 3. "F=Filter/S"  The user has an option of adding the argument     */
  67. /*                  "Filter". The "/S" option tells the ReadArgs()    */
  68. /*                  function that this argument should be treated as  */
  69. /*                  a switch. If the argument is set the switch will  */
  70. /*                  be turned "on", else it will be "off". The "F="   */
  71. /*                  string means that the user also can use the       */
  72. /*                  abbreviation "F" in stead of writing the whole    */
  73. /*                  argument "Filter".                                */
  74.  
  75. #define MY_COMMAND_LINE_TEMPLATE "SoundFile/A,V=Volume/K/N,F=Filter/S"
  76.  
  77. /* Here are some valid command lines:                                 */
  78. /*   Example4 Bird.snd                                                */
  79. /*   Example4 Bird.snd Volume=64                                      */
  80. /*   Example4 Bird.snd Volume 64                                      */
  81. /*   Example4 Bird.snd Filter                                         */
  82. /*   Example4 Bird.snd Volume=64 F                                    */
  83. /*                                                                    */
  84. /* Here are some incorrect command lines:                             */
  85. /*   Example4                     The file name is required!          */
  86. /*   Example4 Bird.snd 64         The keyword "Volume" or "V" must    */
  87. /*                                precede the number 64.              */
  88. /*   Example4 Bird.snd V=5.25     Decimal values may not be used.     */
  89.  
  90.  
  91.  
  92. /* Three command templates are used: */
  93. #define NUMBER_COMMAND_TEMPLATES 3
  94.  
  95. /* The command template numbers: (Where the result of each */
  96. /* command template can be found in the "arg_array".)      */
  97. #define SOUNDFILE_TEMPLATE  0
  98. #define VOLUME_TEMPLATE     1
  99. #define FILTER_TEMPLATE     2
  100.  
  101.  
  102.  
  103. /* Set name and version number: */
  104. UBYTE *version = "$VER: AmigaDOS/ParsingCommandLine/Example4 1.0";
  105.  
  106.  
  107.  
  108. /* Declare an external global library pointer to the Dos library: */
  109. extern struct DosLibrary *DOSBase;
  110.  
  111.  
  112.  
  113. /* Declare a pointer to a RDArgs structure which we will allocate */
  114. /* ourself with help of the AllocDosObject() function:            */
  115. struct RDArgs *my_rdargs;
  116.  
  117.  
  118.  
  119. /* Declared our own functions: */
  120.  
  121. /* Our main function: */
  122. int main( int argc, char *argv[] );
  123.  
  124. /* Cleans up nicely after us: */
  125. void clean_up( STRPTR text, int code );
  126.  
  127.  
  128.  
  129. /* Main function: */
  130.  
  131. int main( int argc, char *argv[] )
  132. {
  133.   /* Simple loop variable: */
  134.   int loop;
  135.  
  136.   /* A pointer to the volume value: */
  137.   LONG *volume_value;
  138.  
  139.   /* Store the pointer which is returned by ReadArgs() here: */
  140.   /* (ReadArgs() returns a pointer to a RDArgs structure if  */
  141.   /* it could successfully parse the command line. Since we  */
  142.   /* have created the RDArgs structure ourself before we     */
  143.   /* call ReadArgs() it will simply return a pointer to the  */
  144.   /* structure which we already have a pointer to. However,  */
  145.   /* we need a separate variable to store the returned value */
  146.   /* in since we need to check if ReadArgs() actually could  */
  147.   /* parse the command line or not. If not NULL is returned. */
  148.   struct RDArgs *temp_rdargs;
  149.  
  150.   /* The ReadArgs() function needs an arrya of LONGs where */
  151.   /* the result of the command parsing will be placed. One */
  152.   /* LONG variable is needed for every command template.   */
  153.   LONG arg_array[ NUMBER_COMMAND_TEMPLATES ];
  154.  
  155.  
  156.  
  157.   /* We need dos library version 37 or higher: */
  158.   if( DOSBase->dl_lib.lib_Version < 37 )
  159.     clean_up( "This program needs Dos Library V37 or higher!", 20 );
  160.  
  161.  
  162.  
  163.   /* We will now clear the "arg_array" (set all values to zero): */
  164.   for( loop = 0; loop < NUMBER_COMMAND_TEMPLATES; loop++ )
  165.     arg_array[ loop ] = 0;
  166.  
  167.  
  168.  
  169.   /* Get a RDArgs structure from AmigaDOS: (We want a RDArgs */
  170.     /* structure with no special tags.)                        */
  171.   my_rdargs = (struct RDArgs *) AllocDosObject( DOS_RDARGS, NULL );
  172.  
  173.   /* Did we get the RDArgs structure? */
  174.   if( !my_rdargs )
  175.     clean_up( "Could not creae the RDArgs structure!", 21 );
  176.  
  177.  
  178.  
  179.   /* If we set the "RDAF_NOPROMPT" flag in the "RDA_Flags" filed of */
  180.   /* the RDArgs structure the user will not be allowed to see the   */
  181.   /* command line template by typing a single question mark (?).    */
  182.   /* If you set this flag the question mark will be accepted as a   */
  183.   /* complete argument if written. Normally you should not turn of  */
  184.   /* this help function! In this example I have therefore put the   */
  185.   /* line inside comment marks. (If you take them away the user     */
  186.   /* will not be be able to see the command line template nor the   */
  187.   /* extra help line defined below.)                                */
  188.   /*                                                                */
  189.   /* my_rdargs->RDA_Flags = RDAF_NOPROMPT;                          */
  190.  
  191.   /* Set an extra help line: (The user can see this help line by    */
  192.   /* typing a question mark (?), so he/she will see the command     */
  193.   /* template line, and then type a question mark again.)           */ 
  194.   my_rdargs->RDA_ExtHelp =  (UBYTE *) "This wasn't much help...";
  195.  
  196.  
  197.  
  198.   /* Parse the command line: (Note that we now use our */
  199.   /* own RDArgs structure which we have prepared.)     */
  200.   temp_rdargs = 
  201.     ReadArgs( MY_COMMAND_LINE_TEMPLATE,
  202.               arg_array,
  203.               my_rdargs
  204.             );
  205.  
  206.   /* Have AmigaDOS successfully parsed our command line? */
  207.   if( !temp_rdargs )
  208.     clean_up( "Could not parse the command line!", 22 );
  209.  
  210.  
  211.  
  212.   /* The comand line has successfully been parsed! */
  213.   /* We can now examine the "arg_array":           */
  214.  
  215.   /* Print template 1, the file name: */
  216.   if( arg_array[ SOUNDFILE_TEMPLATE ] )
  217.     printf( "File name: %s\n", arg_array[ SOUNDFILE_TEMPLATE ] );
  218.  
  219.  
  220.  
  221.   /* Print templat 2, the volume: */
  222.   if( arg_array[ VOLUME_TEMPLATE ] )
  223.   {
  224.     /* Get a pointer to the volume value: */
  225.     volume_value = (LONG *) arg_array[ VOLUME_TEMPLATE ];
  226.  
  227.     /* Print the volume: */
  228.     printf( "Volume: %ld\n", *volume_value );
  229.   }
  230.   else
  231.     printf( "No volume was set\n" );
  232.  
  233.  
  234.  
  235.   /* Print template 2, the filter switch: */
  236.   if( arg_array[ FILTER_TEMPLATE ] )
  237.     printf( "The sound filter was turned on!\n" );
  238.   else
  239.     printf( "No sound filter will be used!\n" );
  240.  
  241.  
  242.  
  243.   /* Before our program terminates we have to free the data that */
  244.   /* have been allocated when we successfully called ReadArgs(): */
  245.   FreeArgs( my_rdargs );
  246.  
  247.   /* The RDArgs structure we allocated will be */
  248.   /* deallocated in the clean_up() function.   */
  249.  
  250.   /* Clean up and exit with a smile on your face! */
  251.   clean_up( "The End", 0 );
  252. }
  253.  
  254.  
  255.  
  256. /* Handy function which closes and deallocates everything */
  257. /* that you have previously opened or allocated. You can  */
  258. /* call this function at any time, and it will clean up   */
  259. /* nicely after you and quit.                             */
  260.  
  261. void clean_up( STRPTR text, int code )
  262. {
  263.   /* Return the RDArgs structure to AmigaDOS: */
  264.   if( my_rdargs )
  265.     FreeDosObject( DOS_RDARGS, my_rdargs );
  266.  
  267.   /* Print the last message: */
  268.   printf( "%s\n", text );
  269.  
  270.   /* Quit: */
  271.   exit( code );
  272. }
  273.  
  274.